package controller.menubar.commands;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.GlobalParams;
import view.MainFrame;

	
public class MkDir implements ActionListener  {
	
	private JTextField path;	
	private MainFrame frame;
	private JFrame newFrame;

	public MkDir(MainFrame frame) {			
		this.frame = frame;			
	}
	
	public void Configure(File file) {
		
		newFrame = new JFrame("Operation Manager");
		newFrame.setLayout(new BorderLayout());
		final File e = file;
		JLabel l = new JLabel(GlobalParams.getInstance().stringCollection.getOther_newDir());
		JButton b = new JButton("Create");
		path = new JTextField(40);		
		b.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (e.canWrite()){
					File newDir = new File(e.getAbsolutePath() + File.separator + path.getText());				
					newDir.mkdir();
					frame.getMainPanel().getPrimary().getDiskContent().CreateNewView(null);
					frame.getMainPanel().getSecondary().getDiskContent().CreateNewView(null);
					newFrame.dispose();
				}else {					
					String op = GlobalParams.getInstance().stringCollection.getOther_write_permission();					 
					JOptionPane.showConfirmDialog(null, op, "Error", JOptionPane.ERROR_MESSAGE);	
				}
			}
		});			
		
		JPanel panel = new JPanel();
		panel.add(l);
		panel.add(path);
		panel.add(b);
		
		newFrame.add(panel, BorderLayout.PAGE_START);		
		newFrame.setLocationRelativeTo(frame);
		newFrame.pack();
		newFrame.setVisible(true);
		newFrame.setLocationRelativeTo(frame);
	}
	
	
	public void actionPerformed(ActionEvent evt) {								
		File actual = frame.getMainPanel().getPrimary().getDiskContent().getActualFile();							
		Configure(actual);
	}		
	
		
		
}	
		
		
		
		
		


	